import { AutoIncrementID } from '@typegoose/auto-increment'
import { prop, plugin, modelOptions } from '@typegoose/typegoose'
import { mongoosePaginate } from '@app/utils/paginate'
import { GENERAL_AUTO_INCREMENT_ID_CONFIG } from '@app/constants/increment.constant'
import {
  IsString,
  MaxLength,
  IsUrl,
  IsEmail,
  IsOptional,
  IsNotEmpty,
} from 'class-validator'
import { getProviderByTypegooseClass } from '@app/transformers/model.transformer'
import { decodeMD5 } from '@app/transformers/codec.transformer'

@plugin(mongoosePaginate)
@plugin(AutoIncrementID, GENERAL_AUTO_INCREMENT_ID_CONFIG)
@modelOptions({
  schemaOptions: {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  },
})
export class Itinerary {
  @MaxLength(20)
  @IsString()
  @IsNotEmpty()
  @prop({ required: true, validate: /\S+/ })
  name: string

  @IsEmail()
  @IsString()
  @IsOptional()
  @prop({ type: String, default: null })
  email: string | null

  @IsUrl({ require_protocol: true })
  @IsString()
  @IsOptional()
  @prop({ type: String, default: null })
  site: string | null

  public get email_hash() {
    const email = this.email?.trim().toLowerCase()
    return email ? decodeMD5(email) : null
  }
}

export const CommentProvider = getProviderByTypegooseClass(Itinerary)
